package _3利用栈进行括号符匹配;

import java.util.Stack;

public class Main {
	public static void main(String[] args) {
		//算法做的复杂了，不推荐看
		Stack s1 = new Stack();
		String ex = "1+[3-(2+6)+{6-2}]-{3+9}";
		for (int i = 0; i <= ex.length() - 1; i++) {
			if ((ex.substring(i, i + 1).equals("(")) 
				|| (ex.substring(i, i + 1).equals("{"))
				|| (ex.substring(i, i + 1).equals("["))) {
					s1.push(ex.substring(i, i + 1));
				for (int j = i; j <= ex.length() - 1; j++) {
					if ((s1.peek().equals("(") && ex.substring(j, j + 1).equals(")"))
						|| (s1.peek().equals("[") && ex.substring(j, j + 1).equals("]"))
						|| (s1.peek().equals("{") && ex.substring(j, j + 1).equals("}"))) {
							s1.pop();
							break;
					}
				}
			}
		}
		
//		while(!s1.isEmpty()){
//			System.out.print(s1.pop()+" ");
//		}
		if (s1.isEmpty()) {
			System.out.println("成功匹配！");
		} else {
			System.out.println("不匹配！");
		}
	}
}
